Fallback 是一個沒有名字、沒有參數也不會回傳任何值的函數,通常在呼叫合約或者是對合約匯款時,沒有與之匹配的函式便會觸發 Fallback Function。
他在以下情況會被執行:
fallback
函式存在時receive()
不存在或 msg.data
非為空時若是合約接收到了 Ether 卻沒有任何的 Fallback Function 也沒有任何呼叫函式的行為,將會 Throw Exception,並退還 Ether。
Fallback 有 2300 gas 限制,也就是說有 Fallback Function 的存在可以保證函數執行的花費控制在 2300 gas 以內。同時我們在佈署合約之前也必須確實的測試 Fallback Function 以把函式的執行花費控制住。
pragma solidity ^0.8.10;
contract Fallback {
event Log(uint gas);
// Fallback function must be declared as external.
fallback() external payable {
// send / transfer (forwards 2300 gas to this fallback function)
// call (forwards all of the gas)
emit Log(gasleft());
}
// Helper function to check the balance of this contract
function getBalance() public view returns (uint) {
return address(this).balance;
}
}
contract SendToFallback {
function transferToFallback(address payable _to) public payable {
_to.transfer(msg.value);
}
function callFallback(address payable _to) public payable {
(bool sent, ) = _to.call{value: msg.value}("");
require(sent, "Failed to send Ether");
}
}
Fallback Function 有以下的特性:
external
payable
的 Function 符合調用,那就會觸發例外處理(exception),除非我們擁有 Fallback functioncatch
,當我們沒有和任何payable function互動,或沒有任何函式符合交易的 encoded data field,就會觸發pragma solidity ^0.8.11;
contract FunctionsExample{
mapping(address => uint) public balanceReceived;
address payable owner;
constructor() public {
owner = payable(msg.sender);
}
function getOwner () public view returns(address){
return owner;
}
function convertWeiToEther(uint _amountInWei) public pure returns(uint){
return _amountInWei / 1 ether;
// alternatively:
// return _amountInWei / 10**18;
// pure function call only interacte with the variables in this scope like _amountInWei, but not the state variables outside the scope
}
function destroyContract() public {
require(msg.sender == owner, "You are not the owner");
selfdestruct(owner);
}
function receiveMoney() public payable {
assert(balanceReceived[msg.sender] + msg.value >= balanceReceived[msg.sender]);
balanceReceived[msg.sender] += msg.value;
}
function withdrawMoney(address payable _to, uint _amount) public {
require(balanceReceived[msg.sender] <= _amount, "You don't have enough ether");
assert(balanceReceived[msg.sender] >= balanceReceived[msg.sender] - _amount);
balanceReceived[msg.sender] -= _amount;
_to.transfer(_amount);
}
fallback () external payable{
receiveMoney();
// fallback function will have input fill(in remix IDE) even we didn't declare any input arguments in function
// because the fallback function is triggered automatically no matter have arguments or not.
// arguments data is in msg.data
}
}
Fallback Function 存在最大的原因是我們沒辦法避免接收 ether,常見情況下我們有至少三種的方式可以接收來自外部的 ether:
selfdistructor
解構其他合約並把自己的合約地址當作參數傳入最糟的情況下,我們還可以依賴2300 gas的限制:
_contractAddress.trasfer(1 ether);
當Contract Data被呼叫時使其強制地避免函式執行
require(msg.data.length == 0)
receive()
和 fallback()
其實差不多,只是當 msg.data
是空的時候就會呼叫 receive()
,但當然合約裡面必須要實作 receive()
才有辦法使用。也就是說在合約裡面想要使用 receive()
來接收 ether 必須同時滿足兩個條件:有實作 receive()
且 msg.data
為空,否則接收到的 ether 都會是呼叫 fallback()
。
例如我希望有一個偵測使用者是否「單純匯款」的註冊系統可以這樣寫:
receive() external payable {
if (msg.value == 0.001 ether) {
deposited[msg.sender] = true;
}
}
最後歡迎大家拍打餵食大學生
0x2b83c71A59b926137D3E1f37EF20394d0495d72d